﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PK10Server
{
    public partial class server : Form
    {
        public server()
        {
            InitializeComponent();            
            textBoxURL.Text = "http://hh2.sad-2314.85641dtqs.com/";
            webBrowser1.Navigate(textBoxURL.Text);           
            //webBrowser1.           
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

        }

        /*public string GetXML()
        {
            WebResponse response = null;
            Stream stream = null;
            StreamReader reader = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://hh2.sad-2314.85641dtqs.com/");

                response = request.GetResponse();
                stream = response.GetResponseStream();

                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    //SaveBinaryFile(response);
                    return null;
                }

                string buffer = "", line;

                reader = new StreamReader(stream);

                while ((line = reader.ReadLine()) != null)
                {
                    buffer += line + "\r\n";
                }

                //SaveTextFile(buffer);
                return buffer;
            }
            catch (Exception e)
            {
            }
            return "";
        }*/

        private void Qi_TextChanged(object sender, EventArgs e)
        {

        }
            
        private void buttonRefresh_Click(object sender, EventArgs e)//刷新
        {
            System.Windows.Forms.HtmlElement he = webBrowser1.Document.Window.Frames["topFrame"].Document.Window.Frames["mainFrame"].Document.Body.Children["lt"];
            if (he.GetAttribute("value") != "3")
                he.SetAttribute("value", "3");
            else
                he.SetAttribute("value", "1");
            he.RaiseEvent("onchange");
        }

        private void buttonGetData_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.HtmlDocument document =
                webBrowser1.Document.Window.Frames["topFrame"].Document.Window.Frames["mainFrame"].Document;
            //textBoxURL.Text = document.Body.InnerHtml;
            //webBrowser1.Document.Window.ScrollTo(0, webBrowser1.Document.Window.Size.Height);//垂直滚动条          
            paserLotteryData(document);
        }

        public void paserLotteryData(HtmlDocument doc)
        {                    
            Regex re = new Regex(@"\r\n");
            string[] line = re.Split(doc.Body.InnerText.ToString()); 
            
            for (int i = 1; i < 10; i++)  //期数可变
            {//39877212-08 日 21:4810小雙虎龍虎龍龍
                string qiShu = line[i].Substring(0, 6);//Int32.Parse
                string guanYaHe = line[i].Substring(19, 2);
                string guan = line[i].Substring(21, 1);//大單 虎龍龍虎龍
                string ya = line[i].Substring(22, 1);//單
                string longHu1 = line[i].Substring(23, 1);
                string longHu2 = line[i].Substring(24, 1);
                string longHu3 = line[i].Substring(25, 1);
                string longHu4 = line[i].Substring(26, 1);
                string longHu5 = line[i].Substring(27, 1);
            }

        }

        private void buttonLoad_Click(object sender, EventArgs e)//加载
        {
            webBrowser1.Navigate(textBoxURL.Text);
        }

        private void buttonJudgeChar_Click(object sender, EventArgs e)
        {
            Application.Run(new judgeCharacter());
        }

        private void buttonLogin_Click(object sender, EventArgs e)  //暂 自动填充用户名密码
        {
            //webBrowser1.Document.All[""

//假设有一个最简单的登录页面，输入用户名密码，点“登录”按钮即可登录。已知用户名输入框的id（或Name，下同）是username，
//            密码输入框的id是password，“登录”按钮的id是submitbutton，那么我们只需要在webBrowser的DocumentCompleted事件中使用下面的代码即可：

//HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"]; 
HtmlElement tbUserid = webBrowser1.Document.All["loginName"]; 
HtmlElement tbPasswd = webBrowser1.Document.All["loginPwd"]; 
if (tbUserid == null || tbPasswd == null ) 
return; 
tbUserid.SetAttribute("value", "okk8877"); 
tbPasswd.SetAttribute("value", "qqaa2211"); 
//btnSubmit.InvokeMember("click");
        }

        private void buttonStartServer_Click(object sender, EventArgs e)
        {
            serverSocket server = new serverSocket();
        }
    }
}
